package com.xiaotu.util;

import cn.hutool.core.util.StrUtil;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

/**
 * 文件相关的工具
 * 包含上传、下载、删除、导入、导出、预览、
 *
 * @author xuchangjian
 */
public class FileUtil {

    /**
     * 上传文件
     * 该方法只针对单文件的上传，适配前台的异步上传文件请求
     *
     * @param storePath 文件存储路径，不带有文件名
     * @return 文件的名称和存储路径信息，
     * 当key="fileRealName"时value为上传的文件本来名称，
     * 当key="fileStoreName"时value为上传的文件存储名称，
     * 当key="storePath"时，value为文件存储在服务器上的路径
     * 当key="size"时，value为文件大小
     * @throws FileUploadException
     * @throws IOException
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     */
    public static final Map<String, String> uploadFile(String oldFilePath, MultipartFile file, String storePath)
            throws FileUploadException, UnsupportedEncodingException,
            FileNotFoundException, IOException {
        String fileRealName = "";
        String fileStoreName = "";
        String size = "";
        byte[] bytes = file.getBytes();
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        if (bytes.length != 0) {
            //文件真实名称
            fileRealName = file.getOriginalFilename();
            String suffix = "";
            if (fileRealName.lastIndexOf(".") != -1) {
                suffix = fileRealName.substring(fileRealName.lastIndexOf("."));
            }
            size = file.getSize() + "";
            // 生成文件存储名称
            fileStoreName = UUIDUtil.getId() + suffix;
            //上传文件到服务器
            File folder = new File(storePath);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            File uploadedFile = new File(storePath + fileStoreName);
            FileCopyUtils.copy(bytes, uploadedFile);
        }
        //保存文件基本信息
        Map<String, String> map = new HashMap<String, String>();
        map.put("fileRealName", fileRealName);
        map.put("fileStoreName", fileStoreName);
        //map.put("storePath", storePath);
        //map.put("size", size);
        //最后将旧的文件删除
        if (StrUtil.isNotEmpty(oldFilePath))
            FileUtil.deleteFile(storePath + oldFilePath);
        return map;
    }

    /**
     * 删除附件
     */
    public static void deleteFile(String storePath) {
        File file = new File(storePath);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 是否是图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static boolean isPicture(String file) {
        boolean flag = false;
        try {
            BufferedImage is = ImageIO.read(new File(file));
            if (is != null) {
                flag = true;
            }
        } catch (IOException e) {
        }
        return flag;
    }

}
